package com.app.base.adapter;


import java.util.Collection;
import java.util.List;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.app.base.adapter.holder.BaseHolder;
import com.app.base.adapter.holder.FooterHolder;

/**
 * 
 * 带有头和脚的Adapter
 */
public class BaseHeadFootAdapter<T> extends BaseClickRecyclerAdapter<T> {

	protected final static int VIEW_HEADER = -1;
	protected final static int VIEW_FOOTER = -2;
	public static enum LoadState {
		//LOADING 在加载更多时设置
		//ERROR 在加载更多失败时设置
		//EMPTY 默认的状态
		LOADING, ERROR, EMPTY;
	}

	/**mResFootViewId的备份*/
	private int mLastResFootViewId;
	public LoadState mLoadState = LoadState.EMPTY;
	
	//用于加载更多
	protected int mPageCount;
	protected int mPageNo;
	
	//头角的ViewHolder 和 Class对象
	protected BaseHolder<T> mFooterHolder;
	protected BaseHolder<T> mHeaderHolder;
	protected Class clazzHeader;
	protected Class clazzFooter;

	//头部的数据
	private Object headData;

	protected OnLoadMoreListener<T> onLoadMoreListener;

	public interface OnLoadMoreListener<T> {
		/**
		 * 翻页加载更多<P>
		 * */
		void onLoadMore(int starIndex);
	}

	public void setOnLoadMoreListener(OnLoadMoreListener<T> listener) {
		this.onLoadMoreListener = listener;
	}

	@Override
	public void setDatas(List<T> items) {
		mLoadState = LoadState.EMPTY;
		super.setDatas(items);
	}
	@Override
	public void setDatas(List<T> items, boolean isClear) {
		mLoadState = LoadState.EMPTY;
		super.setDatas(items, isClear);
	}

	@Override
	public boolean addAll(Collection<? extends T> collection) {
		mLoadState = LoadState.EMPTY;
		return super.addAll(collection);
	}

	/**
	 * 调用这个函数说明会有翻页加载，如果已经到底（没数据了），就把最后一个View给移除掉
	 * 
	 * @param pageCount
	 * @param pageNo
	 */
	public void setPageInfo(int pageCount, int pageNo) {
		if (pageNo >= pageCount) {
			if(mResFootViewId!=-1){
				//如果存在脚View,并且下一页已经没有数据
				notifyItemRangeRemoved(getItemCount() - 1, 1);
				mResFootViewId =-1;
			}
		}else {
			mResFootViewId = mLastResFootViewId;
		}
		this.mPageCount = pageCount;
		this.mPageNo = pageNo;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemCount() {
		int mCount = super.getItemCount();
		if (mResHeadViewId != -1)
			mCount++;
		if (mResFootViewId != -1)
			mCount++;
		return mCount;
	}

	@Override
	public int getItemViewType(int position) {
		if (mResHeadViewId != -1 && position == 0)
			return VIEW_HEADER;
		if (mResFootViewId != -1 && position == getItemCount() - 1)
			return VIEW_FOOTER;
		return super.getItemViewType(position);
	}

	@Override
	public BaseHolder<T> onCreateViewHolder(ViewGroup viewGroup, int viewType) {
		switch (viewType) {
		case VIEW_HEADER:
			if (mHeaderHolder == null) {
				View viewHeader = LayoutInflater.from(viewGroup.getContext())
						.inflate(mResHeadViewId, viewGroup, false);
				mHeaderHolder = getBaseHolder(viewHeader, clazzHeader);
			}
			return mHeaderHolder;
		case VIEW_FOOTER:
			if (mFooterHolder == null) {
				View viewFooter = LayoutInflater.from(viewGroup.getContext())
						.inflate(mResFootViewId, viewGroup, false);
				mFooterHolder = getBaseHolder(viewFooter, clazzFooter);
			}
			return mFooterHolder;
		default:
			return super.onCreateViewHolder(viewGroup, viewType);
		}
	}

	@Override
	public void onBindViewHolder(BaseHolder<T> viewHolder, int position) {
		// 脚状态
		if (mResFootViewId != -1 && (position == getItemCount() - 1)) {
			if (mLoadState == LoadState.ERROR) {
				((FooterHolder) viewHolder).showErrorView();
				viewHolder.getRootView().setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if(onLoadMoreListener!=null&&mLoadState == LoadState.ERROR){
							mLoadState = LoadState.LOADING;
							notifyItemChanged(getItemCount() - 1);
							onLoadMoreListener.onLoadMore(mItems.size() + 1);
							
						}
					}
				});
				viewHolder.getRootView().setTag(position);
			} else if (mLoadState == LoadState.EMPTY) {
				// 防止每次滑到底部时多次加载
				if (onLoadMoreListener != null && mPageNo < mPageCount) {
					onLoadMoreListener.onLoadMore(mItems.size() + 1);
				} else {
					// 到底后隐藏最后一个
					 mResFootViewId = -1;
					// ((FooterHolder)viewHolder).getLoadingText().setText("已经没有数据了");
				}
			} else if (mLoadState == LoadState.LOADING) {
				((FooterHolder) viewHolder).showLoadingView();
			}
			return;
		}

		if (mResHeadViewId != -1) {
			if (position == 0) {
				mHeaderHolder.setHeaderData(headData);
			}
			if (position >= 1) {
				viewHolder.setData(mItems.get(position - 1),position);// 减去头部
			}
			viewHolder.getRootView().setTag(position);
			viewHolder.getRootView().setOnClickListener(this);
		} else {
			if (position >= mItems.size())
				return;
			super.onBindViewHolder(viewHolder, position);
		}
	}
	
	
	public BaseHeadFootAdapter(int viewRes, Class<? extends BaseHolder<T>> clazz) {
		super(viewRes, clazz);
	}

	public void setHeaderData(Object headData) {
		this.headData = headData;
		notifyItemChanged(0);
	}

	public Object getHeaderData() {
		return headData;
	}
	
	public BaseHolder<T> getHeadViewHolder(){
		return mHeaderHolder;
	}
	public void setHeadViewId(int resId, Class clazzHeader) {
		mResHeadViewId = resId;
		this.clazzHeader = clazzHeader;
	}

	public void setFootViewId(int resId, Class clazzFooter) {
		mLastResFootViewId = mResFootViewId = resId;
		this.clazzFooter = clazzFooter;
	}
	
}
